home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3221 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.1 KB  |  117 lines

  1. Path: atglab.bls.com!Alun.Champion
  2. From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [Q]Assigning function pointer in C/C++.
  5. Date: 22 Jan 1996 22:32:40 GMT
  6. Organization: Computer People Inc.
  7. Message-ID: <ALUN.CHAMPION.96Jan22173240@g7240065.bridge.bst.bls.com>
  8. References: <DL3JJu.5nB.0.queen@torfree.net> <4doc42$gsb@bmdhh222.bnr.ca>
  9.     <ALUN.CHAMPION.96Jan19113523@g7240065.bridge.bst.bls.com>
  10.     <4e09re$kit@populus.slu.se>
  11. NNTP-Posting-Host: bstfirewall.bst.bls.com
  12. In-reply-to: Thomas.Johansson@stax.slu.se's message of 22 Jan 1996 15:18:06
  13.     GMT
  14.  
  15. In article <4e09re$kit@populus.slu.se> Thomas.Johansson@stax.slu.se (Thomas Johansson) writes:
  16. : In article <ALUN.CHAMPION.96Jan19113523@g7240065.bridge.bst.bls.com>, Alun.Champion@bridge.bst.bls.com (Alun Champion) says:
  17.  
  18. :> You can have pointers to member functions providing you know what class 
  19. :> (or base class) you are going to be assigning functions from.
  20. :> 
  21. :>    int (A::*p)(void);
  22. :> 
  23. :> declares p as a pointer to an A member function which takes nothing and
  24. :> returns an int. The usage of p requires an object or pointer to an object of
  25. :> type A or derived from A. i.e:
  26. :>   
  27. :>    A a;
  28. :>    int z = (a.*p)();    // Uses '.*' operator.
  29. :> or
  30.  
  31. : Is there any way that you can declare a variable to hold the resulting 
  32. : pointer, or must it be 'used' right away ?
  33. : something like
  34.  
  35. :  typdef int (A::*p)(void) Fp; // Fp is a type of 'pointer to member function'
  36.  
  37. :  Fp f = (a.*p);               // the object a (of class A) has a member p
  38. :  int z = f();
  39.  
  40. : I cannot make this work with Borland 4.5.
  41.  
  42. I think you may have misunderstood my original post.
  43.  
  44. Example:
  45.  
  46.   #include <iostream.h>
  47.  
  48.   class A
  49.   {
  50.     public:
  51.       A(int a = 0) : a_(a)
  52.       { }
  53.  
  54.       int func(void)
  55.       { return a_; }
  56.  
  57.       int foo(void)
  58.       { return -a_; }
  59.  
  60.     private:
  61.       int a_;
  62.   };
  63.  
  64.   int
  65.   main(void)
  66.   {
  67.       A a(5);
  68.       int (A::*p)(void) = A::func;    // p is a variable pointing to a member
  69.                                         // function of A returning an int
  70.       cout << (a.*p)() << endl;        // apply p to object a.
  71.  
  72.       p = A::foo;            // re-assign the variable p.
  73.       cout << (a.*p)() << endl;
  74.  
  75.       return 0;
  76.   }
  77.  
  78. Sorry, it just doesn't work the way you would like it. You can only make
  79. a pointer to the class method and not to the instance method. Therefore
  80. you need an instance to apply this method on.
  81.  
  82. You could try a small class to do this for you:
  83.  
  84.   template <class T>
  85.   class IntFunctor 
  86.   {
  87.     public
  88.       IntFunctor(T& object, int (T::*method)(void))
  89.         : object_(object), method_(method)
  90.       { }
  91.  
  92.       int operator () (void)
  93.       { return (obj.*method)(); }
  94.  
  95.     private:
  96.       T& object_;
  97.       int (T::*method_)(void);
  98.   };
  99.  
  100. Then you could use it like:
  101.  
  102.   A a;
  103.   IntFunctor f(a, A::func);
  104.   ...
  105.   int z = f();
  106.  
  107. But it is not very generic and seems a bit of a hassle, and it has some
  108. potential risks, like the instance goes away and a use of the Functor after
  109. that would have undefined results (usually at least crashing the program)';
  110.  
  111. Hope this clears things up.
  112. Regards
  113.  
  114.    -A.
  115. -- 
  116. | A.Champion                |
  117.